package com.hifar.test.entrust.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.sys.MinioUtil;
import com.hifar.test.entrust.constant.EntrustConsts;
import com.hifar.test.entrust.mapper.HfEnvGkProjectMapper;
import com.hifar.test.entrust.mapper.HfEnvProjectMapper;
import com.hifar.test.entrust.pojo.HfEnvGkProject;
import com.hifar.test.entrust.pojo.HfEnvProject;
import com.hifar.test.entrust.pojo.VO.HfEnvGkProjectSaveVO;
import com.hifar.test.entrust.service.IHfEnvGkProjectService;
import com.hifar.test.entrust.service.IHfEnvProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 委托项目 服务实现类
 * </p>
 *
 * @author wei
 * @since 2024-12-04 15:51:56
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class HfEnvGkProjectServiceImpl extends ServiceImpl<HfEnvGkProjectMapper, HfEnvGkProject> implements IHfEnvGkProjectService {

    @Autowired
    private IBaseOssFilesService baseOssFilesService;

    @Override
    public void removeByEntrustId(String entrustId) {
        QueryWrapper<HfEnvGkProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvGkProject::getEntrustId, entrustId);
        this.remove(queryWrapper);
    }

    @Override
    public void removeByGkEntrustId(String gkEntrustId) {
        QueryWrapper<HfEnvGkProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvGkProject::getGkEntrustId, gkEntrustId);
        this.remove(queryWrapper);
    }

    @Override
    public List<HfEnvGkProject> listByEntrustId(String entrustId) {
        QueryWrapper<HfEnvGkProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvGkProject::getEntrustId, entrustId)
                .orderByAsc(HfEnvGkProject::getRowSort);
        return this.list(queryWrapper);
    }


    @Override
    public void updateEntrustId(String gkEntrustId, String entrustId) {
        this.lambdaUpdate()
                .set(HfEnvGkProject::getEntrustId, entrustId)
                .eq(HfEnvGkProject::getGkEntrustId, gkEntrustId)
                .update();
    }

    @Override
    public List<HfEnvGkProject> listByGkEntrustId(String gkEntrustId) {
        QueryWrapper<HfEnvGkProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvGkProject::getGkEntrustId, gkEntrustId)
                .orderByAsc(HfEnvGkProject::getRowSort);
        return this.list(queryWrapper);
    }

    @Override
    public List<HfEnvGkProjectSaveVO> listVOByGkEntrustId(String gkEntrustId) {
        QueryWrapper<HfEnvGkProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvGkProject::getGkEntrustId, gkEntrustId)
                .orderByAsc(HfEnvGkProject::getRowSort);

        List<HfEnvGkProject> gkProjectList = this.list(queryWrapper);
        List<HfEnvGkProjectSaveVO> gkProjectSaveVOList = new ArrayList(gkProjectList.size());
        for (HfEnvGkProject hfEnvGkProject : gkProjectList) {
            String id = hfEnvGkProject.getId();
            List<BaseOssFiles> testPhotoList = MinioUtil.listByRefId(id, EntrustConsts.WTD_TEST_PHONE);
            HfEnvGkProjectSaveVO gkProjectSaveVO = new HfEnvGkProjectSaveVO();
            gkProjectSaveVO.setTestPhotoList(testPhotoList);
            BeanUtils.copyProperties(hfEnvGkProject, gkProjectSaveVO);
            gkProjectSaveVOList.add(gkProjectSaveVO);
        }

        return gkProjectSaveVOList;
    }
}
